package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.User;
import com.example.domain.Category;
import com.example.dto.CategoryAddDTO;
import com.example.exception.BusinessException;
import com.example.service.CategoryService;
import com.example.service.OperationLogService;
import com.example.mapper.UserMapper;
import com.example.mapper.CategoryMapper;
import com.example.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import java.util.List;
import java.util.Map;

/**
* @author HP
* @description 管理员服务实现
* @createDate 2025-08-18
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<UserMapper, User> implements CategoryService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private OperationLogService operationLogService;

    @Override
    public Result addCategory(CategoryAddDTO categoryAddDTO) {
        // 验证分类信息
        // 检查分类是否已存在
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", categoryAddDTO.getName());
        Category existingCategory = categoryMapper.selectOne(queryWrapper);
        if (existingCategory != null) {
            throw new BusinessException("分类已存在");
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryAddDTO, category);
        int insert = categoryMapper.insert(category);
        if (insert <= 0) {
            throw new BusinessException("添加分类失败");
        }
        return Result.success();
    }

    @Override
    public Result updateCategory(Category category) {
        if (category == null || category.getId() == null) {
            throw new BusinessException("分类ID不能为空");
        }
        // 检查分类是否存在
        Category existingCategory = categoryMapper.selectById(category.getId());
        if (existingCategory == null) {
            throw new BusinessException("分类不存在");
        }
        int update = categoryMapper.updateById(category);
        if (update <= 0) {
            throw new BusinessException("更新分类失败");
        }
        return Result.success();
    }

    @Override
    public Result deleteCategory(Long id, Long operatorId) {
        if (id == null) {
            throw new BusinessException("分类ID不能为空");
        }
        // 检查分类是否存在
        Category category = categoryMapper.selectById(id);
        if (category == null) {
            throw new BusinessException("分类不存在");
        }
        int delete = categoryMapper.deleteById(id);
        if (delete <= 0) {
            throw new BusinessException("删除分类失败");
        }
        // 记录操作日志
//        operationLogService.recordLog(operatorId, operatorName, "删除分类，ID: " + id + "，名称: " + category.getName(), ip);
        return Result.success();
    }

    @Override
    public Result listAllCategories() {
        List<Category> categories = categoryMapper.selectList(null);
        if (categories == null || categories.isEmpty()) {
            throw new BusinessException("暂无分类数据");
        }
        return Result.success(Map.of("categories", categories));
    }
}